﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Controller;


namespace Web
{
    public partial class _Default : System.Web.UI.Page
    {
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                VendorPOItemController vendorPoItemController = new VendorPOItemController();
                IList<VendorPOItemEntity> vendorPoItemEntities = vendorPoItemController.FindVendorPOItemsWithinDays(7);

                ContractDueController contractDueController = new ContractDueController();
                IList<ContractDueEntity> contractDueEntities = contractDueController.FindContractDueByLots(2);

                if ((vendorPoItemEntities == null || vendorPoItemEntities.Count == 0) && (contractDueEntities == null || contractDueEntities.Count == 0))
                {
                    Session["FlashMessage"] = "Notice: No Outstanding Alert Within These Period!";
                    Session["FlashMessageType"] = "notice";
                }
                else
                {
                    VendorPOItemGridView.DataSource = vendorPoItemEntities;
                    VendorPOItemGridView.DataBind();

                    CustomerPOItemGridView.DataSource = contractDueEntities;
                    CustomerPOItemGridView.DataBind();
                    
                }
                
                
                ListItemCollection l = new ListItemCollection();
                for(int i=1;i<=30;i++)
                {
                    ListItem li=new ListItem(i.ToString(),i.ToString());
                    l.Add(li);
                }

                DaysDropDownList.DataSource = l;
                DaysDropDownList.DataBind();
                DaysDropDownList.SelectedIndex = 6;
                DaysLabel.Text = "7";

                ListItemCollection lotsList = new ListItemCollection();
                for (int i = 1; i <= 10; i++)
                {
                    ListItem li = new ListItem(i.ToString(), i.ToString());
                    lotsList.Add(li);
                }

                LotsDropDownList.DataSource = lotsList;
                LotsDropDownList.DataBind();
                LotsDropDownList.SelectedIndex = 1;
                LotsLabel.Text = "2";
            }

            this.PreRender += new EventHandler(UpdateFlashMessage);
        }
        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }
        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            if(DaysTextBox.Text.Trim().Length>0)
            {
                DaysLabel.Text = DaysTextBox.Text.Trim();
                
            }
            else
            {
                DaysLabel.Text = DaysDropDownList.SelectedValue;
                
            }
            try
            {
                int days = Convert.ToInt32(DaysLabel.Text);
                VendorPOItemController vendorPoItemController = new VendorPOItemController();
                IList<VendorPOItemEntity> vendorPoItemEntities = vendorPoItemController.FindVendorPOItemsWithinDays(days);

                if (vendorPoItemEntities == null || vendorPoItemEntities.Count == 0)
                {
                    Session["FlashMessage"] = "Notice: No Outstanding Alert Within These Period!";
                    Session["FlashMessageType"] = "notice";
                }
                else
                {
                    VendorPOItemGridView.DataSource = vendorPoItemEntities;
                    VendorPOItemGridView.DataBind();
                }
                
            }
            catch (Exception)
            {
                Session["FlashMessage"] = "Error: Cannot Complete Your Request!";
                Session["FlashMessageType"] = "error";
            }
            
        }

        protected void DaysDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DaysLabel.Text = DaysDropDownList.SelectedValue;
            DaysTextBox.Text = DaysDropDownList.SelectedValue;
            try
            {
                int days = Convert.ToInt32(DaysLabel.Text);
                VendorPOItemController vendorPoItemController = new VendorPOItemController();
                IList<VendorPOItemEntity> vendorPoItemEntities = vendorPoItemController.FindVendorPOItemsWithinDays(days);

                if (vendorPoItemEntities == null || vendorPoItemEntities.Count == 0)
                {
                    Session["FlashMessage"] = "Notice: No Outstanding Alert Within These Period!";
                    Session["FlashMessageType"] = "notice";
                }
                else
                {
                    VendorPOItemGridView.DataSource = vendorPoItemEntities;
                    VendorPOItemGridView.DataBind();
                }

            }
            catch (Exception)
            {
                Session["FlashMessage"] = "Error: Cannot Complete Your Request!";
                Session["FlashMessageType"] = "error";
            }

        }


        protected void LotsSubmitButton_Click(object sender, EventArgs e)
        {
            if (LotsTextBox.Text.Trim().Length > 0)
            {
                LotsLabel.Text = LotsTextBox.Text.Trim();

            }
            else
            {
                LotsLabel.Text = LotsDropDownList.SelectedValue;

            }
            try
            {
                int lots = Convert.ToInt32(LotsLabel.Text);

                ContractDueController contractDueController = new ContractDueController();
                IList<ContractDueEntity> contractDueEntities = contractDueController.FindContractDueByLots(lots);

                if (contractDueEntities == null || contractDueEntities.Count == 0)
                {
                    Session["FlashMessage"] = "Notice: No Outstanding Alert Within These Period!";
                    Session["FlashMessageType"] = "notice";
                }
                else
                {
                    CustomerPOItemGridView.DataSource = contractDueEntities;
                    CustomerPOItemGridView.DataBind();
                }

            }
            catch (Exception)
            {
                Session["FlashMessage"] = "Error: Cannot Complete Your Request!";
                Session["FlashMessageType"] = "error";
            }
        }

        protected void LotsDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LotsLabel.Text = LotsDropDownList.SelectedValue;
            LotsTextBox.Text = LotsDropDownList.SelectedValue;
            try
            {
                int lots = Convert.ToInt32(LotsLabel.Text);

                ContractDueController contractDueController = new ContractDueController();
                IList<ContractDueEntity> contractDueEntities = contractDueController.FindContractDueByLots(lots);


                if (contractDueEntities == null || contractDueEntities.Count == 0)
                {
                    Session["FlashMessage"] = "Notice: No Outstanding Alert Within These Period!";
                    Session["FlashMessageType"] = "notice";
                }
                else
                {
                    CustomerPOItemGridView.DataSource = contractDueEntities;
                    CustomerPOItemGridView.DataBind();
                }
            }
            catch (Exception)
            {
                Session["FlashMessage"] = "Error: Cannot Complete Your Request!";
                Session["FlashMessageType"] = "error";
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ExcelGenerator.GenerateExcelForCustomerInvoiceById(2);
        }
    }
}
